package com.spzx.product.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.domain.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.domain.CategoryBrandVo;
import com.spzx.product.domain.vo.CategoryBrandDetailVo;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.mapper.CategoryMapper;
import com.spzx.product.service.CategoryBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.rowset.serial.SerialException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements CategoryBrandService {
    @Autowired
    CategoryBrandMapper categoryBrandMapper;

    @Autowired
    CategoryMapper categoryMapper;
    @Override
    public List<CategoryBrandVo> selectCategoryBrandList(CategoryBrand categoryBrand) {
        List<CategoryBrandVo> categoryBrandVos = categoryBrandMapper.selectCategoryBrandList(categoryBrand);

        return categoryBrandVos;
    }

    @Override
    public CategoryBrandDetailVo getDetailById(Long id) {
        CategoryBrand categoryBrand = baseMapper.selectById(id);
        Long categoryId = categoryBrand.getCategoryId();
        HashMap<String,Long> map = categoryMapper.getDetailById(categoryId);

        List<Long> list = new ArrayList<>();
        list.add(map.get("c1Id"));
        list.add(map.get("c2Id"));
        list.add(map.get("c3Id"));

        CategoryBrandDetailVo vo = new CategoryBrandDetailVo();
        vo.setId(categoryBrand.getId());
        vo.setBrandId(categoryBrand.getBrandId());
        vo.setCategoryId(categoryBrand.getCategoryId());
        vo.setCategoryIdList(list);

        return vo;
    }


    @Override
    public Map<String,Long> getDetailByC3Id(Long categoryId) {
        HashMap<String,Long> map = categoryMapper.getDetailById(categoryId);


        return map;
    }

    @Override
    public List<Brand> selectBrandById(Long categoryId) {
       return categoryBrandMapper.selectBrandById(categoryId);

    }

    @Override
    public int addCategoryBrand(CategoryBrand categoryBrand) {
        Long categoryId = categoryBrand.getCategoryId();
        Long brandId = categoryBrand.getBrandId();
        Long count = categoryBrandMapper.selectCount(new LambdaQueryWrapper<CategoryBrand>().eq(CategoryBrand::getCategoryId, categoryBrand)
                .eq(CategoryBrand::getBrandId, brandId));
        if (count>0){
            try {
                throw new SerialException("该品牌已添加分类");
            } catch (SerialException e) {
                throw new RuntimeException(e);
            }
        }
       return categoryBrandMapper.insert(categoryBrand);

    }

    @Override
    public int updateCategoryBrand(CategoryBrand categoryBrand) {
        Long id = categoryBrand.getId() == null ? -1L : categoryBrand.getId();

        CategoryBrand dbCategoryBrand = baseMapper.selectOne(new LambdaQueryWrapper<CategoryBrand>(
        ).eq(CategoryBrand::getBrandId, categoryBrand.getBrandId()).eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId()).last("limit 1"));

        if (dbCategoryBrand!=null && !id.equals(dbCategoryBrand.getId())){
                throw new ServiceException("数据已存在");
        }

        return 1;
    }

}
